﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using System.Text;
using Jumbot.Common;
using JumbotCms.Common;
namespace JumbotCms.WebFile.Admin
{
    public partial class _createhtmlajax : JumbotCms.API.AdminCenter
    {
        private string _operType = string.Empty;
        private string _response = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            ChannelId = Str2Str(q("ccid"));
            Admin_Load(ChannelId + "-08", "json", true);
            if (q("oper") == "createtop10")
            {
                string sql = "SELECT TOP 10 [Id],[Title] FROM [jcms_module_" + ChannelType + "] WHERE ([IsPass]=1" +
                    " AND [ChannelId]=" + ChannelId;
                if (q("act") == "good")
                    sql += " And [IsTop]=1";
                sql += ") ORDER BY id Desc";
                doh.Reset();
                doh.SqlCmd = sql;
                DataTable dt = doh.GetDataTable();
                string TempStr = "<li><a href=\"{$_url$}\" title=\"{$_title$}\">{$_title$}</a></li>";
                string ReplaceStr = string.Empty;
                StringBuilder sb = new StringBuilder("");
                if (dt.Rows.Count > 0)
                {
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        ReplaceStr = TempStr;
                        //以下是预处理
                        ReplaceStr = ReplaceStr.Replace("{$_no$}", (j + 1).ToString());
                        ReplaceStr = ReplaceStr.Replace("{$_url$}", Go2View(doh, 1, ChannelIsHtml, ChannelId, dt.Rows[j]["Id"].ToString()));

                        //以下是正规处理
                        for (int i = 0; i < dt.Columns.Count; i++)
                        {
                            ReplaceStr = ReplaceStr.Replace("{$_" + dt.Columns[i].ColumnName.ToLower() + "$}", dt.Rows[j][i].ToString());
                        }
                        sb.Append(ReplaceStr);
                    }
                }
                string js = Server.MapPath(site.Dir) + ChannelDir + "\\js\\top10_" + q("act") + ".js";
                SaveJsFile(Jumbot.Common.Strings.Html2Js(sb.ToString()), js);
                dt.Clear();
                dt.Dispose();
                this._response = JsonResult(1, "更新完成");
            }
            if (q("oper") == "createchannel")
            {
                CreateChannelFile(ChannelId);
                CreateDefaultFile();
                this._response = JsonResult(1, "频道及站点首页更新完成");
            }
            if (q("oper") == "createbyclass")
            {
                if (q("act") == "class")
                {
                    doh.Reset();
                    doh.SqlCmd = "SELECT Id FROM [jcms_normal_class] WHERE [IsOut]=0 AND [ChannelId]=" + ChannelId;
                    if (q("classcode") != "0")
                        doh.SqlCmd += " and [code] LIKE '" + q("classcode") + "%'";
                    doh.SqlCmd += " ORDER BY code";
                    DataTable dtClass = doh.GetDataTable();
                    MakeClass(dtClass);
                    dtClass.Clear();
                    dtClass.Dispose();
                    this._response = JsonResult(1, "栏目更新完成");
                }
                if (q("act") == "content")
                {
                    doh.Reset();
                    doh.SqlCmd = "SELECT id FROM [jcms_module_" + ChannelType + "] WHERE [ChannelId]=" + ChannelId + " and [IsPass]=1";
                    if (q("classcode") != "0")
                        doh.SqlCmd += " and [ClassID] in (Select id FROM [jcms_normal_class] WHERE [IsOut]=0 AND [Code] LIKE '" + q("classcode") + "%')";
                    DataTable dtContent = doh.GetDataTable();
                    MakeView(dtContent);
                    dtContent.Clear();
                    dtContent.Dispose();
                    this._response = JsonResult(1, "内容更新完成");
                }
            }
            if (q("oper") == "createbyid")
            {
                string Sid = Str2Str(q("id1"));
                string Eid = Str2Str(q("id2"));
                string wSql = string.Empty;
                doh.Reset();
                doh.SqlCmd = "SELECT id FROM [jcms_module_" + ChannelType + "] WHERE [ChannelId]=" + ChannelId + " and [IsPass]=1";
                if (Sid != "0")
                {
                    if (Eid == "0")
                        wSql = " And id>=" + Sid;
                    else
                        wSql = " And id between " + Sid + " and " + Eid;
                }
                else
                    return;
                doh.SqlCmd += wSql;
                DataTable dtContent = doh.GetDataTable();
                MakeView(dtContent);
                dtContent.Clear();
                dtContent.Dispose();
                this._response = JsonResult(1, "内容更新完成");
            }
            Response.Write(this._response);
        }
        /// <summary>
        /// 生成栏目页
        /// </summary>
        private void MakeClass(DataTable dt)
        {
            int total = dt.Rows.Count;
            if (total > 0)
            {
                for (int i = 0; i < total; i++)
                {
                    CreateClassFile(dt.Rows[i]["Id"].ToString(), false);
                }
            }
        }
        /// <summary>
        /// 生成内容页
        /// </summary>
        private void MakeView(DataTable dt)
        {
            string ContentId = string.Empty;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ContentId = dt.Rows[i]["Id"].ToString();
                CreateContentFile(ContentId, -1);
            }
        }
    }
}
